/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'company/company/index',
                    add_url: 'company/company/add',
                    edit_url: 'company/company/edit',
                    del_url: 'company/company/del',
//                    multi_url: 'category/multi',
//                    dragsort_url: 'ajax/weigh',
                    table: 'company',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                escape: false,
                pk: 'id',
                sortName: 'id',
                pagination: true,
                commonSearch: true,
                search: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), formatter: Table.api.formatter.search},
                        {field: 'logo', title: __('Logo'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'address', title: __('Address')},
                        {field: 'contact', title: __('Contact'), formatter: Table.api.formatter.search},
                        {field: 'phone', title: __('Phone'), formatter: Table.api.formatter.search},
                        {field: 'start_time', title: __('StartTime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'end_time', title: __('EndTime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'add_time', title: __('AddTime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'status', title: __('Status'), formatter: function(value) {
                                if (value == 1) {
                                    return __('Status1');
                                } else if (value == 2) {
                                    return __('Status2');
                                }
                            }, operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,buttons:[
                                {
                                    name: 'create',
                                    text: __('一键部署'),
                                    title: __('一键部署'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    url: 'company/company/deploy',
                                    success: function (data, ret) {
                                        // console.log(data);
                                        // console.log(ret);
                                        // console.log(ret.code);
                                        if(ret.code == 1){
                                            Layer.alert("<button onclick=\"test("+ret['data']+")\">点击复制代码</button>：<span id=>"+ret["data"]+"</span>");
                                        }
                                        // else {
                                        //     Layer.alert('<span id="" style="color: red;">'+ret["msg"]["msg"]+'</span>');
                                        // }
                                    },
                                },
                            ], formatter: Table.api.formatter.operate},
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function(params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },
        add: function() {
            Controller.api.bindevent();
            setTimeout(function() {
                $("#c-type").trigger("change");
            }, 100);
        },
        edit: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                $(document).on("change", "#c-type", function() {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                custom: function(value, row, index) {
                    //console.log(row)
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="ranking/lists/change" data-params="' + row.status + '" data-id="' + row.id + '"><i class="fa ' + (row.status == 2 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
                _vote_log: function(value, row, index) {
                    return '<span data-itemize="advisory" data-id="' + row['id'] + '" onclick="open_details(this)"><a href="javascript:;"  class=" addtabsit " title="详细线索">' + value + '</a></span>';
//                    return '<a class="btn-change text-success" data-url="ranking/lists/change" data-params="' + row.status + '" data-id="' + row.id + '"><i class="fa ' + (row.status == 2 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            }
        }
    };
    return Controller;

});

function open_details(e) {
//    var url = "https://www.baidu.com";
//    var url = /*$.fn.bootstrapTable.defaults.extend.cluedetails +*/ '/admin/report/enrollmentclue/cluedetails/?itemize='+itemize+'&id='+id+'&admin='+admin_id+'&store='+store_id+'&city='+city_id+'&area='+area_id+'&source_type='+source_type;
//    $(e).find('a').attr('href',url);
//    $(e).find('a').click();
}

function test(js){
    alert(js);
}


